import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import "./plugins/element.js";
import "./plugins/bootstrap"
import axios from "axios";
import "./assets/css/global.css";
 // axios.defaults.baseURL = "http://101.200.150.138:8088"
axios.defaults.baseURL = "http://localhost:8088"
// axios.defaults.baseURL = "http://8.131.51.190:8088"
axios.interceptors.request.use(request => {
    request.headers.Authorization = window.sessionStorage.getItem('token')
    if (!request.headers.Authorization) {
        if (window.location.pathname.indexOf("adminMain") > -1 || window.location.pathname.indexOf("adminLogin") > -1) {
            Vue.prototype.$myRouter.push("/adminLogin")
        } else {
            Vue.prototype.$myRouter.push("/userLogin")
        }
    }
    return request;
});

axios.interceptors.response.use(response => {
    if (response.headers.token) {
        window.sessionStorage.setItem('token', response.headers.token)
    }
    return response;
}, error => {
    const {response} = error
    switch (response.status) {
        case 401:
            if (window.location.pathname.indexOf("adminMain") > -1 || window.location.pathname.indexOf("adminLogin") > -1) {
                Vue.prototype.$myRouter.push("/adminLogin")
            } else {
                Vue.prototype.$myRouter.push("/userLogin")
            }
            Vue.prototype.$notify.error({
                title: "错误",
                message: "未登录或登录过期，请登录"
            })
            window.sessionStorage.removeItem('token')
            store.state.userId = ''
            store.state.userName = ''
            store.state.userImg = ''
            store.state.userStatus = true
            break
        case 404:
            Vue.prototype.$notify.error({
                title: "错误",
                message: "网络错误"
            })
            break
        case 500:
            Vue.prototype.$notify.error({
                title: "错误",
                message: "服务器繁忙"
            })
            break
    }
    return Promise.reject(error)
});

Vue.prototype.$http = axios;
Vue.config.productionTip = false; //阻止控制台警告

new Vue({
    router,
    store,
    render: h => h(App),
}).$mount("#app");
